<?php 
$docRoot = $_SERVER ['DOCUMENT_ROOT'] . '/' . strtok ( $_SERVER ['PHP_SELF'], '/' );
include_once ($docRoot . "/application/service/feed/loader/FeedLoader.php");
include_once ($docRoot . "/application/service/db/TournamentDAO.php");
?>
<?php

class TournamentLoaderImpl implements FeedLoader{
	
	private $tournamentFeedUrl;
	private $feedLocalXmlName;
	private $feedLocalPath;

	/**
	 * @param field_type $feedLocalXmlName
	 */
	public function setFeedLocalXmlName($feedLocalXmlName) {
		$this->feedLocalXmlName = $feedLocalXmlName;
	}

	/**
	 * @param field_type $feedLocalPath
	 */
	public function setFeedLocalPath($feedLocalPath) {
		$this->feedLocalPath = $feedLocalPath;
	}

	/**
	 * @param field_type $tournamentFeedUrl
	 */
	public function setTournamentFeedUrl($tournamentFeedUrl) {
		$this->tournamentFeedUrl = $tournamentFeedUrl;
	}

	/* (non-PHPdoc)
	 * @see FeedLoader::loadFeed()
	 */
	public function loadFeed() {
		// TODO Auto-generated method stub
	
		$tournamentFeedContent = file_get_contents ( $this->tournamentFeedUrl );
		
		$tournamentXmlObject = simplexml_load_string ( $tournamentFeedContent );
		
		$attributeId = "id";
		$attributeName = "league";
		
		$tourId = $tournamentXmlObject->children()->attributes()->$attributeId;
		$tourName = $tournamentXmlObject->children()->attributes()->$attributeName;

		mkdir ( $this->feedLocalPath . $tourId, 0700 );
		
		$tournamentFeedSaveFullPath = $this->feedLocalPath . $tourId . $this->feedLocalXmlName;
		
		$fh = fopen ( $tournamentFeedSaveFullPath, 'w' );
		fwrite ( $fh, $tournamentFeedContent );
		fclose ( $fh );
		
		$tournameDao = new TournamentDAO();
		$tournameDao->addTournament($tourId, $tourName, $this->tournamentFeedUrl);
		
	}


}

?>